package com.edu.dao.impl;

import com.edu.bean.Book;
import com.edu.bean.User;
import com.edu.dao.BaseDao;
import com.edu.dao.BookDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:max
 * @Date: 2024/9/14 17:04
 * @Description:
 */


public class BookDaoImpl implements BookDao {
    @Override
    public List<Book> selectAllBooks() {

        Connection conn= BaseDao.getConn();
        PreparedStatement s = null;
        ResultSet rs = null;
        try {
            if(conn!=null) {
                System.out.println("连接成功");
            }
            String sql = "select * from books";
//            System.out.println(sql);
            s = conn.prepareStatement(sql);
            rs =  s.executeQuery();
            List<Book> books=new ArrayList<Book>();
            while(rs.next()) {
                Book bk=new Book();
                bk.setBookId(rs.getInt("bookId"));
                bk.setBookName(rs.getString("bookName"));
                bk.setBookType(rs.getString("bookType"));
                bk.setWriter(rs.getString("bookWriter"));
                books.add(bk);
            }
            return books;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(conn, s,rs);
        }
        return null;
    }
}
